package org.summerframework.component.note.config;

import com.aliyuncs.IAcsClient;
import com.github.qcloudsms.SmsSingleSender;
import org.summerframework.component.note.manager.SendNoteService;
import org.summerframework.component.note.manager.impl.AliyunSendNoteServiceImpl;
import org.summerframework.component.note.manager.impl.NopSendNoteServiceImpl;
import org.summerframework.component.note.manager.impl.NoteManagerSupport;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Repository;
import org.summerframework.component.note.manager.impl.TencentSendNoteServiceImpl;

/**
 * @author 石超
 * @version v1.0.0
 */
@Configuration
@ComponentScan({
        "org.summerframework.component.note.*.service",
        "org.summerframework.component.note.*.controller"
})
@MapperScan(value = "org.summerframework.component.note.*.model",
        annotationClass = Repository.class)
@AutoConfigureAfter(ValidationAutoConfiguration.class)
@EnableConfigurationProperties(NoteProperties.class)
public class NoteAutoConfiguration {

    @Bean
    public NoteManagerSupport noteManagerSupport() {
        return new NoteManagerSupport();
    }

    @Configuration
    @ConditionalOnClass(IAcsClient.class)
    @EnableConfigurationProperties(AliyunNoteProperties.class)
    public static class AliyunNoteConfiguration {

        @Bean
        public SendNoteService aliyunSendNoteService(AliyunNoteProperties aliyunNoteProperties) {
//            ValidationUtils.validate(aliyunNoteProperties);
            return new AliyunSendNoteServiceImpl(aliyunNoteProperties);
        }
    }
    @Configuration
    @ConditionalOnClass(SmsSingleSender.class)
    @EnableConfigurationProperties(TencentNoteProperties.class)
    public static class TencentNoteConfiguration {

        @Bean
        public SendNoteService tencentSendNoteService(TencentNoteProperties tencentNoteProperties) {
            return new TencentSendNoteServiceImpl(tencentNoteProperties);
        }
    }

    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    @ConditionalOnProperty(name = "note.enable-test", havingValue = "true")
    public SendNoteService sendNoteService() {
        return new NopSendNoteServiceImpl();
    }
}
